<?php
namespace app\api\controller;

use app\common\model\AgentManage;
use app\common\model\Area;
use think\Db;
use think\Request;

class Ad extends Base
{

    public function getAreaId($jing = '116.65716', $wei = '39.90966')
    {
        $key = 'SZSBZ-LFV33-YHK3A-YZZ77-YCVAQ-V4BYJ';
        $url = 'http://apis.map.qq.com/ws/geocoder/v1/?location=';
        $request = $url . $wei . ',' . $jing . '&key=' . $key;
        $res = json_decode(file_get_contents($request), true);
        $name = '%' . $res['result']['ad_info']['city'] . ',' . $res['result']['ad_info']['district'];
        $areaid = Area::where('merger_name', 'like', $name)->value('id');
        $agentid = AgentManage::where('agem_region', $areaid)->value('agem_id');
        return $agentid;
    }
    /**
     * @name 根据定位获取广告
     * @return array 所有广告
     */
    public function getAllAd()
    {

        $time = time();
        $arr = [];
        $res=\app\common\model\Ad::where('ad_status',3)->where('ad_start_time<'.$time.' and ad_end_time>'.$time)->update(['ad_status'=>1]);

        $jing = $this->request->param('jing', '116.65716');
        $wei = $this->request->param('wei', '39.90966');
        if ((!empty($jing)) && (!empty($wei))) {
            $agentid = $this->getAreaId($jing, $wei);
            $url = Db::table('rbw_ad')->where('ad_agents_id', $agentid)->where('ad_status',1)->where('ad_start_time<'.$time.' and ad_end_time>'.$time)->field('ad_label,ad_image_url,ad_content')->select();
            foreach ($url as $k => $v) {
                if ($url[$k]['ad_label'] == 1) {
                    $arr['banner'][] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . $v['ad_image_url'];
                    var_dump($arr['banner']);
                }
                if ($url[$k]['ad_label'] == 2) {
                    $arr['text_ads'][] = $v['ad_content'];
                }
                if ($url[$k]['ad_label'] == 3) {
                    $arr['foot_ads'][] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . $v['ad_image_url'];
                }
            }
            if (!isset($arr['banner'])) {
                $banner = Db::table('rbw_ad')->where('ad_agents_id', null)->where('ad_status',1)->where('ad_label', 1)->field('ad_image_url,ad_content')->select();
                foreach ($banner as $k => $v) {
                    $arr['banner'][] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . $v['ad_image_url'];
                }

            }

            if (!isset($arr['text_ads'])) {
                $banner = Db::table('rbw_ad')->where('ad_agents_id', null)->where('ad_status',1)->where('ad_label', 2)->field('ad_image_url,ad_content')->select();
                foreach ($banner as $k => $v) {
                    $arr['text_ads'][] = $v['ad_content'];
                }
            }
            if (!isset($arr['foot_ads'])) {
                $banner = Db::table('rbw_ad')->where('ad_agents_id', null)->where('ad_status',1)->where('ad_label', 3)->field('ad_image_url,ad_content')->select();
                foreach ($banner as $k => $v) {
                    $arr['foot_ads'][] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . $v['ad_image_url'];
                }
            }
            return $this->getOutput(200, 'ok', $arr);
        } else {
            return $this->getOutput(400, 'no', '服务器错误,请稍后重试!');
        }
    }
}
